extends Node
class_name WoodStackLogic

# 引用参数
@export var wood_config: WoodStackConfig
var in_wood_mushroom_data: Array[MushroomLogic] = []
var mushroom_data: MushroomLogic
var wood_manager: WoodStackManager
# 内部逻辑使用参数
var total_output_amount: int = 0
var in_wood_mushroom_amount: int
var is_wood_full: bool = false
var old_wood_stat:bool = false
var seats: Array[MushroomSeat] = []
var available_seats: Array[MushroomSeat] = []

# 信号
signal wood_seat_stat_changed(wood:Node)
signal seat_booked_by_mushroom(mushroom: Node, seat: Node)



# 初始化
func _ready():
	add_to_group("woodstack")
	get_seat_info()
	update_in_wood_mushroom()
	wood_manager.mushroom_go_woodstack.connect(func(mushroom, _wood): _on_mushroom_seating(mushroom))
	for seat in seats:
		seat.seat_stat_changed.connect(_on_seat_stat_changed)
			
			
			

# 座位方面
# 管理座位
func get_seat_info():
	for child in get_children():
		if child is MushroomSeat:
			seats.append(child)

# 当收到管理器信号时，分配蘑菇人到座位
func _on_mushroom_seating(mushroom):
	for seat in seats:
		if seat.is_seat_taken == false:
			seat_booked_by_mushroom.emit(mushroom, seat)
	return
	


# 当座位状态变动时，查看是否更新状态；若更新则通知管理器
func _on_seat_stat_changed():
	update_in_wood_mushroom()
	var empty_seats_count = 0
	for seat in seats:
		if not seat.is_seat_taken:
			empty_seats_count += 1
	is_wood_full = (empty_seats_count ==0)
	if is_wood_full != old_wood_stat:
		wood_seat_stat_changed.emit(self)
		old_wood_stat = is_wood_full
	return

# 工作方面
# 更新当前木桩中蘑菇人信息：
func update_in_wood_mushroom():
	for seat in seats:
		if seat.on_seat_mushroom != null:
			var current_mushroom = seat.on_seat_mushroom
			in_wood_mushroom_data.append(current_mushroom)
	get_total_output_amout()



# 获取蘑菇人的配置数据
func get_output_amount_single():
	return mushroom_data.mushroom_config.mushroom_output_amount

# 计算最终的产出量
func get_total_output_amout():
	for mushroom in in_wood_mushroom_data:
		total_output_amount += get_output_amount_single()
	return total_output_amount
	
# 根据时间产出


	
